﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Sale.Report
{
    public partial class 产品销售价格收入年报 : System.Web.UI.Page
    {
        protected string year;
        protected string month;
        protected string RiQiStart;
        protected string RiQiEnd;
        protected string RiQi;


        /// <summary>
        /// 读入所有的SESSION数据，向VIEW暴露数据项
        /// </summary>
        protected void read_session()
        {
            year = DList_year.SelectedValue;
        }

        private string sql = @"
SELECT 
MONTH(日期) AS 月份,
dbo.产品.品名, 

/*销售量*/
SUM(数量) AS 销售量, 

/*累计销售量*/
(select 
  SUM(数量) 
from
 dbo.产品销售合同
where
 MONTH(日期)<=MONTH(h.日期)
 and
 (产品ID = h.产品ID)
) AS 累计销售量,

/*销售金额*/
SUM(金额) AS 销售金额,


/*累计销售金额*/
(select 
  SUM(金额) 
from
 dbo.产品销售合同
where
 MONTH(日期)<=MONTH(h.日期)
 and
 (产品ID = h.产品ID)
) AS 累计销售金额

FROM  dbo.产品销售合同 AS h INNER JOIN
      dbo.产品 ON h.产品ID = dbo.产品.ID
WHERE YEAR(日期)=*年份*
GROUP BY 产品ID,dbo.产品.品名,MONTH(日期)";
        protected static DateTime date;

        //SQL的初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";

            
            if (!this.IsPostBack)
            {
                DList_year.SelectedValue = DateTime.Now.Year.ToString();              
                sql = sql.Replace("*年份*", DList_year.SelectedValue);
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
                read_session();
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }

        //年份变化时
        protected void DList_year_SelectedIndexChanged(object sender, EventArgs e)
        {
            sql = sql.Replace("*年份*", DList_year.SelectedValue);
            this.SqlDataSource1.SelectCommand = sql;
            this.ViewState["SelectCommand"] = sql;
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }


    }
}
